package com.songqintuan.common.validator;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import com.songqintuan.common.constant.GlobalConstants;
import com.songqintuan.common.util.MessageFactory;

public class ImageValidator implements Validator,Serializable {
	protected  boolean isEmpty(String s)
	{
		return ((s.equals(null)) || (s.length()==0));
	}
	protected boolean isWhitespace (String s)
	{
	  String  whitespace = " \t\n\r";
	  int i;
	  for (i = 0; i < s.length(); i++)
	   {
	       char c = s.charAt(i);
	       if (whitespace.indexOf(c) >= 0)
		   {
			  
	    	   return true;
			  
		   }
	   }

	     return false;
	}
	protected boolean isCharsInBag (String s, String bag)
	{
	  int i;

	  for (i = 0; i < s.length(); i++)
	  {

	      char c = s.charAt(i);
	      if (bag.indexOf(c) == -1) return false;
	  }
	  return true;
	}

	
	public void validate(FacesContext context, UIComponent component, Object obj)
			throws ValidatorException {
		// TODO Auto-generated method stub
		String imageName=(String)obj;
		FacesMessage message;
		String label = (String)component.getAttributes().get("label");
		label = (null == label)?"":label;
		if(!isEmpty(imageName))
		{
			if (imageName.length()<=4)
		 	{
				message = MessageFactory.getMessage(GlobalConstants.USER_BUNDLE_NAME, "image.length.error.message", FacesMessage.SEVERITY_ERROR, label, imageName.length());
		 		throw new ValidatorException(message);
		    }
			if (imageName.length()>50){
				message = MessageFactory.getMessage(GlobalConstants.USER_BUNDLE_NAME, "image.length.error.message", FacesMessage.SEVERITY_ERROR, label, imageName.length());
		 		throw new ValidatorException(message);
		 	}
			if ( isWhitespace(imageName) )
			{
				message = MessageFactory.getMessage(GlobalConstants.USER_BUNDLE_NAME, "input.blank.error.message", FacesMessage.SEVERITY_ERROR, label);
		 		throw new ValidatorException(message);
			}
			int pos1 = imageName.lastIndexOf(".");
			if ((pos1 <= 0)||(pos1 ==imageName.length()))
			{
				message = MessageFactory.getMessage(GlobalConstants.USER_BUNDLE_NAME, "input.invalidate.error.message", FacesMessage.SEVERITY_ERROR, label);
		 		throw new ValidatorException(message);
				
			}else {
				
				String suffixStr= imageName.substring(pos1 + 1);
				if (!("jpg".equals(suffixStr.toLowerCase()) || "jpeg".equals(suffixStr.toLowerCase())|| "gif".equals(suffixStr.toLowerCase()))) {
					message = MessageFactory.getMessage(GlobalConstants.USER_BUNDLE_NAME, "input.invalidate.error.message", FacesMessage.SEVERITY_ERROR, label);
			 		throw new ValidatorException(message);
				}
			}
			if ( !isCharsInBag( imageName, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_"))
			{
				
				message = MessageFactory.getMessage(GlobalConstants.USER_BUNDLE_NAME, "input.invalidate.error.message", FacesMessage.SEVERITY_ERROR, label);
		 		throw new ValidatorException(message);
				
			}
		}

	}

}

